<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>13、回调函数执行次数</title>
</head>
<body>
    <div id="test"></div>

    <script type="text/javascript" src="../js/react.development.js"></script>
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <!-- 对props属性类型，以及是否必传进行限制 -->
    <script type="text/javascript" src="../js/prop-types.js"></script>
    
    <script type="text/babel"> 
        class Demo extends React.Component{
            state = {isHot:true}
            showData = () =>{
                const {input1} = this
                alert(input1.value)
            }
            showData2 = () =>{
                const {input2} = this
                alert(input2.value)
            }
            changeWeather = () =>{
                const {isHot} = this.state
                this.setState({isHot:!isHot})
            }
            saveData = (c) =>{
                this.input2 = c
                console.log('@',c)
            }
            render(){
                const {isHot} = this.state
                // 当dom更新时，如果使用内联回调ref，则会执行两次，第一次将原函数赋为null，第二次将新的函数替换掉之前的函数
                // 第二个input只会执行一次ref，使用外部回调
                return(
                    <div>
                        <h2 onClick={this.changeWeather}>今天天气很{isHot ? '炎热' : '凉爽'}</h2>
                        <input ref={(c) => {this.input1 = c; console.log('@',c)}} type="text" placeholder="点击按钮提示数据" />
                        <button onClick={this.showData}>点我提示左侧的数据</button>
                        <input ref={this.saveData} type="text" onBlur={this.showData2} placeholder="失去焦点提示数据" />
                    </div>
                )
            }
        }

        ReactDOM.render(<Demo />, document.getElementById('test'))
    </script>
</body>
</html>